package com.up.blog.controller;

import com.alibaba.fastjson.JSON;
import com.up.blog.domain.Infobean;
import com.up.blog.service.LoginCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
@Controller

public class LoginController {
    @Autowired
    private LoginCheckService loginCheck;
    @RequestMapping(value = "/login", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String login(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession httpSession = req.getSession();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Infobean infobean = new Infobean();
        String ret=null;
        try {
            String headimg = loginCheck.login(username, password);
            if (headimg.length() > 0) {
                infobean.setCode(0);
                infobean.setMsg("登录成功！");
            } else {
                infobean.setCode(1);
                infobean.setMsg("账户或密码错误！");
            }
            ret=JSON.toJSONString(infobean);
            httpSession.setAttribute("rname", username);
            httpSession.setAttribute("headimg", headimg);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

}
